
import { pAjax } from "../../index/javascripts/gather";
import { Big  } from "./big";
//获取元素
const contentBox = document.querySelector('.content')
//获取商品id
let id = window.sessionStorage.getItem('goodsId') - 0

fun()
async function fun() {
    let res = await pAjax({
        url: 'http://localhost:8888/goods/item',
        data: { id },
        dataType: 'json'
    })
    

    let str = `
        <div class="left">
            <div class="show">
                <img src="${res.info.img_big_logo}" alt="">
                <div class="mask"></div>
            </div>
            <div class="enlarge">
                <img src="${res.info.img_big_logo}" alt="">
            </div>
        </div>
        <div class="right">
            <p class="title">${res.info.title}</p>

            <p class="price">
                ￥${res.info.current_price}
            </p>
            <button data-id=${res.info.goods_id}>加入购物车</button>
        </div>
    `
    contentBox.innerHTML = str


    let a = new Big(".left")
    console.log(a);



    const descBox = document.querySelector('.desc');
    descBox.innerHTML = res.info.goods_introduce

    const aa = document.body;
    aa.addEventListener('click', e => {

        e = e || window.event
        let target = e.target || e.srcElement
            if (target.nodeName === 'BUTTON') {
                addCart(target.dataset.id)
            }

            
    })
   

    async function addCart(goodsId) {
        // 首先要回去奥id和token
        let id = window.localStorage.getItem('id')
        let token = window.localStorage.getItem('token')

        // 判断id和token
        if (!id || !token) {
            // 按照我们以前的思路就是去到登录页面 去登录
            // 我现在在的也面是商品列表页面
            // 让用户有一个选择的过程
            if (window.confirm('您还没有登录点取消留在本页面,点击确定去到登录页面')) {
                window.location.href = './login.html'
                return
            }
        }



        // 发送请求
        let res = await pAjax({
            url: 'http://localhost:8888/cart/add',
            method: 'post',
            data: { id, goodsId },
            dataType: 'json',
            headers: { authorization: token }
        })
        console.log(res);

        if(res.code == 0){
            if(window.confirm('您还没有登录点取消留在本页面,点击确定去到登录页面')){
                window.location.href = './login.html'
                return
            }        
        }else{
            alert('oka')
        }
    }
}


